DLSS.Registration = Ext.extend(Ext.Panel, {
    constructor:function(){
        var couplesGrid = new Ext.grid.EditorGridPanel({
            cellCls:'couple-management-table',
            store: DLSS.couplesStore,
            cm: DLSS.couplesColumnModel,
            width:800,
            height:275,
            style:'margin-left:50px;',
            autoExpandColumn:'number',
            title:'Couples',
            frame:true,
            viewConfig: {forceFit: true},
            clicksToEdit:2,
            tbar: [{
                text: 'Add',
                handler : function(){
                    var u = new DLSS.Couple({number: '1',name:'',level:'',city:'',club:'',trainer:''});
                    couplesGrid.stopEditing();
                    DLSS.couplesStore.insert(0, u);
                    couplesGrid.startEditing(0, 0);
                    couplesGrid.fireEvent('afteredit',{record:u, grid:couplesGrid, row:0});
                }
            },{
                text: 'Remove',
                handler:function() {
                    if (!couplesGrid.getSelectionModel().hasSelection()) {
                        Ext.Msg.show({
                            title:'Information',
                            msg: 'First select a couple.',
                            buttons: Ext.Msg.OK,
                            animEl: couplesGrid.getTopToolbar().items.get(1).getEl(),
                            icon: Ext.Msg.INFO
                        });
                        return;
                    }
                    var row = couplesGrid.getSelectionModel().getSelectedCell()[0];
                    var r = couplesGrid.getStore().getAt(row);
                    Ext.Msg.show({
                        title:'Warning!',
                        msg: 'Are you really want to delete couple '+r.get('number')+'?',
                        buttons: Ext.Msg.YESNO,
                        fn: function(buttonId){
                            if (buttonId!='yes') return;
                            Ext.Ajax.request({
                                url:'coupleDelete.htm',
                                params:{
                                    id:this.r.get('id')
                                },
                                success:function() {
                                    this.s.remove(this.r);
                                },
                                scope:this,
                                method:'POST'
                            })
                        },
                        scope:{r:r,s:couplesGrid.getStore()},
                        animEl: couplesGrid.getTopToolbar().items.get(1).getEl(),
                        icon: Ext.Msg.QUESTION
                    });
                }
           }]
           
        });

        couplesGrid.on('afteredit', this.submitCouples,this);

 		var adjudicatorsGrid = new Ext.grid.EditorGridPanel({
            cellCls:'adjudicator-management-table',
            store: DLSS.adjudicatorsStore,
            cm: DLSS.adjudicatorsColumnModel,
            width:500,
            height:275,
            style:'margin-left:50px;',
            autoExpandColumn:'number',
            title:'Adjudicators',
            frame:true,
            viewConfig: {
                forceFit: true
            },
            clicksToEdit:2,
            tbar: [{
                text: 'Add',
                handler : function(){
                    var u = new DLSS.Adjudicator({name: '', letter:''});
                    adjudicatorsGrid.stopEditing();
                    DLSS.adjudicatorsStore.insert(0, u);
                    adjudicatorsGrid.startEditing(0, 0);
                    adjudicatorsGrid.fireEvent('afteredit',{record:u, grid:adjudicatorsGrid, row:0});
                }
            },{
                text: 'Remove',
                handler:function() {
                    if (!adjudicatorsGrid.getSelectionModel().hasSelection()) {
                        Ext.Msg.show({
                            title:'Information',
                            msg: 'First select an adjudicator.',
                            buttons: Ext.Msg.OK,
                            animEl: adjudicatorsGrid.getTopToolbar().items.get(1).getEl(),
                            icon: Ext.Msg.INFO
                        });
                        return;
                    }
                    var row = adjudicatorsGrid.getSelectionModel().getSelectedCell()[0];
                    var r = adjudicatorsGrid.getStore().getAt(row);
                    Ext.Msg.show({
                        title:'Warning!',
                        msg: 'Are you realy want to delete adjudicator '+r.get('name')+'?',
                        buttons: Ext.Msg.YESNO,
                        fn: function(buttonId){
                            if (buttonId!='yes') return;
                            Ext.Ajax.request({
                                url:'adjudicatorDelete.htm',
                                params:{
                                    id:this.r.get('id')
                                },
                                success:function() {
                                    this.s.remove(this.r);
                                },
                                scope:this,
                                method:'POST'
                            })
                        },
                        scope:{r:r, s:adjudicatorsGrid.getStore()},
                        animEl: adjudicatorsGrid.getTopToolbar().items.get(1).getEl(),
                        icon: Ext.Msg.QUESTION
                    });
                }
            }]
        });

        adjudicatorsGrid.on('afteredit', this.submitAdjudicators,this);
        
        var dancesGrid = new Ext.grid.EditorGridPanel({
            cellCls:'dance-management-table',
            store: DLSS.dancesStore,
            cm: DLSS.dancesColumnModel,
            width:300,
            height:150,
            style:'margin-left:50px; margin-top:20px;',
            autoExpandColumn:'name',
            title:'Dances',
            frame:true,
            viewConfig: {
                forceFit: true
            },
            clicksToEdit:2,
            tbar: [{
                text: 'Add',
                handler : function(){
                    var u = new DLSS.Dance({name: '', abbreviation:''});
                    dancesGrid.stopEditing();
                    DLSS.dancesStore.insert(0, u);
                    dancesGrid.startEditing(0, 0);
                    dancesGrid.fireEvent('afteredit',{record:u, grid:dancesGrid, row:0});
                }
            },{
                text: 'Remove',
                handler:function() {
                    if (!dancesGrid.getSelectionModel().hasSelection()) {
                        Ext.Msg.show({
                            title:'Information',
                            msg: 'First select a dance.',
                            buttons: Ext.Msg.OK,
                            animEl: dancesGrid.getTopToolbar().items.get(1).getEl(),
                            icon: Ext.Msg.INFO
                        });
                        return;
                    }
                    var row = dancesGrid.getSelectionModel().getSelectedCell()[0];
                    var r = dancesGrid.getStore().getAt(row);
                    Ext.Msg.show({
                        title:'Warning!',
                        msg: 'Are you realy want to delete dance '+r.get('name')+'?',
                        buttons: Ext.Msg.YESNO,
                        fn: function(buttonId){
                            if (buttonId!='yes') return;
                            Ext.Ajax.request({
                                url:'danceDelete.htm',
                                params:{
                                    id:this.r.get('id')
                                },
                                success:function() {
                                    this.s.remove(this.r);
                                },
                                scope:this,
                                method:'POST'
                            })
                        },
                        scope:{r:r, s:dancesGrid.getStore()},
                        animEl: dancesGrid.getTopToolbar().items.get(1).getEl(),
                        icon: Ext.Msg.QUESTION
                    });
                }
            }]
        });

        dancesGrid.on('afteredit', this.submitDances,this);


        DLSS.Registration.superclass.constructor.call(this,{
            frame:false,
            header:false,
            border:false,
            layout:'table',
            bodyStyle:'padding:10px;',
            layoutConfig:{columns:2},
            items:[
                couplesGrid,
                new Ext.Panel({
					frame:false,
            		header:false,
            		border:false,
            	    width:550,
				    height:650,
				    style:'margin-top:5px;',
				    items:[
						adjudicatorsGrid,
                		dancesGrid
                	]
                })                    
            ],
            tbar:DLSS.COMPONENTS_ROOT.getToolBarForPage(1)
        });

        this.on('show',function() {
            //DLSS.couplesStore.load();
            //DLSS.adjudicatorsStore.load();
        },this);
    },
    submitCouples:function(editEvent) {
        var r = editEvent.record;
        if (r.get('number').trim()==''||
            r.get('name').trim()=='' ||
            r.get('level').trim()==''||
            r.get('city').trim()==''||
            r.get('club').trim()==''||
            r.get('trainer').trim()=='') {
            /*this.markInvalid(
                editEvent.grid,
                editEvent.row,
                false,
                "Couple number and name<br>must not be empty."
            );*/
            return;
        }
        /*if (editEvent.grid.getStore().query('number', r.get('number'), false, false).getCount()>1) {
            this.markInvalid(
                editEvent.grid,
                editEvent.row,
                false,
                "Couple number must be unique."
            );
            return;
        }*/
        DLSS.markInvalid(editEvent.grid, editEvent.row,true);

        Ext.Ajax.request({
            url:'coupleUpdate.htm',
            maskEl:editEvent.grid.getGridEl(),
            params:{
                id:r.get('id'),
                number:r.get('number').trim(),
                name:r.get('name').trim(),
                level:r.get('level').trim(),
                city:r.get('city').trim(),
                club:r.get('club').trim(),
                trainer:r.get('trainer').trim()
            },
            method:'POST',
            success:function(rsp) {
                var o = Ext.util.JSON.decode(rsp.responseText);
                if (o.success) {
                    DLSS.couplesStore.reload();
                } else {
                    Ext.Ajax.fireEvent('requestexception');
                }
            },
            scope:r
        });
    },
    submitAdjudicators:function(editEvent) {
        var r = editEvent.record;
        if (r.get('name').trim()==''||
            r.get('letter').trim()=='') {
            	//DLSS.markInvalid(editEvent.grid, editEvent.row, false, "Adjudicator name and letter<br>must not be empty.");
            return;
        }
        if (editEvent.grid.getStore().query('name', r.get('name'), false, false).getCount()>1) {
            DLSS.markInvalid(editEvent.grid, editEvent.row, false, "Adjudicator name must be unique.");
            return;
        }
        DLSS.markInvalid(editEvent.grid, editEvent.row,true);

        Ext.Ajax.request({
            url:'adjudicatorUpdate.htm',
            maskEl:editEvent.grid.getGridEl(),
            params:{
                id:r.get('id'),
                name:r.get('name').trim(),
                letter:r.get('letter').trim()
            },
            method:'POST',
            success:function(rsp) {
                var o = Ext.util.JSON.decode(rsp.responseText);
                if (o.success) {
                    DLSS.adjudicatorsStore.reload();
                } else {
                    Ext.Ajax.fireEvent('requestexception');
                }
            },
            scope:r
        });
    },
    submitDances:function(editEvent) {
        var r = editEvent.record;
        if (r.get('name').trim()==''||
            r.get('abbreviation').trim()=='') {
            return;
        }
        if (editEvent.grid.getStore().query('name', r.get('name'), false, false).getCount()>1) {
            DLSS.markInvalid(editEvent.grid, editEvent.row, false, "Dance name must be unique.");
            return;
        }
        DLSS.markInvalid(editEvent.grid, editEvent.row,true);

        Ext.Ajax.request({
            url:'danceUpdate.htm',
            maskEl:editEvent.grid.getGridEl(),
            params:{
                id:r.get('id'),
                name:r.get('name').trim(),
                abbreviation:r.get('abbreviation').trim()
            },
            method:'POST',
            success:function(rsp) {
                var o = Ext.util.JSON.decode(rsp.responseText);
                if (o.success) {
                    DLSS.dancesStore.reload();
                } else {
                    Ext.Ajax.fireEvent('requestexception');
                }
            },
            scope:r
        });
    }
    
});

